package grouplife::Controller::Topic;
use Moose;
use namespace::autoclean;
use Catalyst::Request::Upload;
use Catalyst::Log;
use grouplife::Model::CommentedItem::CommentedItemModel;
use grouplife::Model::Topic::TopicCreateModel;
use grouplife::Model::Topic::TopicSearchModel;
use grouplife::Model::Users::UsersSearchModel;

BEGIN {extends 'Catalyst::Controller'; }
__PACKAGE__->config( 'Plugin::ConfigLoader' => { file => 'grouplife.yml' } );

=head1 NAME

grouplife::Controller::Users - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut

sub index :Path :Args(0) {
        my ($self, $c) = @_;

	my $users= $c->model('Topic::TopicSearchModel')->find_last_upd_user($c);
	$c->stash(users=> $users);
        $c->stash(title => '讨论区');
        $c->stash(css => 'discuss.css'),
	$c->stash(login_user => $c->session->{login_user});
	$c->stash(javascript => '/topic/discuss.js');
        $c->stash(template => 'topic/discuss.tt2');
}

sub form_create :Chained('base') :PathPart('form_create') :Args(0) {
	my ($self, $c) = @_;

        if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                $c->flash->{redirect_after_login} = '/topic/form_create';
                return 0;
        }

	my $host_uri = $c->config->{host_uri};
	$c->stash(title => '发起新话题');
	$c->stash(css => 'fabu.css'),
	$c->stash(host_uri  => $host_uri),
	$c->stash(login_user => $c->session->{login_user});
	$c->stash(javascript => '/topic/form_create.js');
	$c->stash(template => 'topic/form_create.tt2');
}

sub topic_total:Local {
        my ($self, $c) = @_;

        $c->res->output($c->model('Topic::TopicSearchModel')->get_topic_count());
}

sub topic_paging :Local {
        my ($self, $c) = @_;

        my $start_index = $c->request->params->{start_index};
        my $end_index = $c->request->params->{end_index};
        my $page = $start_index / 20;
        $page = $page + 1;

        $c->stash(
                login_user => $c->session->{login_user},
                nowrapper => 1,
                template => 'topic/topic_search.tt2',
                topics => $c->model('Topic::TopicSearchModel')->topic_search($page)
        );
}

sub fabu :Local {
        my ($self, $c) = @_;

        if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                $c->flash->{redirect_after_login} = '/topic/form_create';
                return 0;
        }
        my $biaoti = $c->request->params->{biaoti};
        my $neirong = $c->request->params->{neirong};

	my $commented_item = $c->model('CommentedItem::CommentedItemModel')->create('5');

        my $topic = $c->model('Topic::TopicCreateModel')->create($biaoti, $neirong, $commented_item->id, $c->user->id);

	$c->user->add_to_my_news({user_id=>$c->user->id, photo=>$c->user->photo, event_type_id=>8, me_name=>$c->user->username, target_id=>$topic->id, target_name=>$topic->title});

	$c->response->redirect($c->uri_for($c->controller('Topic')->action_for('topic_info'), [$topic->id]));
}

sub topics {
        my ($self, $c) = @_;

        my @topics = $c->model('Topic::TopicSearchModel')->get_all();
	$c->stash(title => '讨论区');
	$c->stash(css => 'topic.css'),
	$c->stash(javascript => '/topic/topic_main.js');
	$c->stash(template => 'topic/topic_mainpage.tt2');
}

sub base :Chained('/') :PathPart('topic') :CaptureArgs(0) {
        my ($self, $c) = @_;
}

sub object :Chained('base') :PathPart('id') :CaptureArgs(1) {
        my ($self, $c, $id) = @_;
        $c->stash(object => $c->model('Topic::TopicSearchModel')->find($id));
        $c->stash(id => $id);
}

sub topic_info :Chained('object') :PathPart('topic_info') :Args(0) {
	my ($self, $c) = @_;

	my $login = 'N';
	if (defined $c->session->{login_user}) {
                $login = 'L';
        }

	my @comments = $c->model('Comments::CommentsForTopicModel')->get_comments($c->stash->{object}->comm_item_id);
	$c->stash(comments => @comments) if(scalar(@comments) != 0);
	$c->stash(login_user => $c->session->{login_user});
	$c->stash(javascript => 'topic/topic_all.js',
		topic => $c->stash->{object},
		login => $login,
		css=>'topic_mainpage.css',
		template => 'topic/topic_mainpage.tt2');
}

sub comment:Chained('base') :PathPart('comment') :Args(0){
        my ( $self, $c ) = @_;
        if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                return 0;
        }

        my $comment = $c->request->params->{comment}  || '';
        my $comm_id= $c->request->params->{comm_id} || '';
        my $topic_id= $c->request->params->{topic_id} || '';
	my $topic = $c->model('Topic::TopicSearchModel')->find($topic_id);
	$topic->update({"lupd_user_id" => 0});
	$topic->update({"lupd_user_id" => $c->session->{login_user}->id});
        $c->model('CommentedItem::CommentedItemModel')->add_to_comments($comm_id, $c->user->get('id'), $comment);

        $c->stash(comments => $c->model('Comments::CommentsForTopicModel')->get_comments($comm_id),
                nowrapper=> 1,
                javascript => '/topic/topic_all.js',
                template => 'topic/new_comment.tt2');
}


=head1 AUTHOR

lijie,,,

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

1;
